package org.uniport;

import javax.swing.*;
import java.awt.*;
import java.sql.Date;

import org.uniport.factory.*;
import org.uniport.vo.*;

public class InputUserData extends JFrame{
	
	private JTextField tf_email = new JTextField(30);
	private JTextField tf_f_name = new JTextField(30);
	private JTextField tf_l_name = new JTextField(30);
	private JTextField tf_d_birth = new JTextField(25);
	private JTextField tf_u_password = new JTextField(30);
	private JTextField tf_uni = new JTextField(30);
	private JTextField tf_major = new JTextField(30);
	private JTextField tf_pic = new JTextField(30);
	private JLabel l_rs = new JLabel();
	public InputUserData(){

		JButton btn_create = new JButton("Create");
		btn_create.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				Photo photo = new Photo();
				User user = new User();	
				String email = tf_email.getText();
				String f_name = tf_f_name.getText();
				String l_name = tf_l_name.getText();
				Date d_birth = Date.valueOf(tf_d_birth.getText());
				String u_password = tf_u_password.getText();
				String uni = tf_uni.getText();
				String major = tf_major.getText();
				String pic = tf_pic.getText();
				
				photo.setPho_addr(email+"\\"+pic);
				user.setEmail(email);
				user.setF_name(f_name);
				user.setL_name(l_name);
				user.setD_birth(d_birth);
				user.setU_password(u_password);
				user.setUni(uni);
				user.setMajor(major);
				try{
					if(PhotoDAOFactory.getIPhotoDAOInstance().addPhoto(photo)){
						System.out.println("Photo created.");
						user.setPic_id(PhotoDAOFactory.getIPhotoDAOInstance().findPhotoID(photo.getPho_addr()));
						if(UserDAOFactory.getIUserDAOInstance().addUser(user)){
							System.out.println("User created.");
							l_rs.setText("User: "+user.getEmail()+" created.");
						}
					}
				}catch(Exception e1){
					e1.printStackTrace();
				}
				
			}	
		});
		setBounds(100,100,500,600);
        setLayout(new FlowLayout(FlowLayout.LEFT));
        add(new JLabel("E-mail: ",JLabel.CENTER));
        add(tf_email);
        add(new JLabel("First Name: ",JLabel.CENTER));
        add(tf_f_name);
        add(new JLabel("Last Name: ",JLabel.CENTER));
        add(tf_l_name);
        add(new JLabel("Birthday(YYYY-MM-DD): ",JLabel.CENTER));
        add(tf_d_birth);
        add(new JLabel("Password: ",JLabel.CENTER));
        add(tf_u_password);
        add(new JLabel("Univercity: ",JLabel.CENTER));
        add(tf_uni);
        add(new JLabel("Study Major: ",JLabel.CENTER));
        add(tf_major);
        add(new JLabel("Picture: ",JLabel.CENTER));
        add(tf_pic);
        add(btn_create);
        add(l_rs);
        setDefaultCloseOperation(EXIT_ON_CLOSE );
        setVisible(true);
	}
	
	
	
	public static void main(String[] args) {
		new InputUserData();

		
	}

}
